<template>
	<view class="wrap">
		<u-form :model="form" ref="uForm" :error-type="errorType">
			<view class="top">
				<view class="item">
					<view class="left">收货人</view>
					<input type="text" placeholder-class="line" v-model="form.name" placeholder="请填写收货人姓名" />
				</view>
				<view class="item">
					<view class="left">手机号码</view>
					<input type="text" placeholder-class="line" v-model="form.phone" placeholder="请填写收货人手机号" />
				</view>
				<view class="item" @tap="showRegionPicker">
					<view class="left">所在地区</view>
					<input disabled type="text" placeholder-class="line" v-model="address" placeholder="省市区县、乡镇等" />
				</view>
				<view class="item address">
					<view class="left" @tap="getRegeo">详细地址<u-icon name="map-fill"></u-icon></view>
					<textarea type="text" placeholder-class="line" v-model="form.address" placeholder="街道、楼牌等" >
					</textarea>
				</view>
			</view>
			<view class="bottom">
				<view class="default">
					<view class="left">
						<view class="set">设置默认地址</view>
						<view class="tips">提醒：每次下单会默认推荐该地址</view>
					</view>
					<view class="right">
						<switch color="red" @change="setDefault" :checked="checked" />
					</view>
				</view>
			</view>
		</u-form>
		<view style="text-align: center;">
			<u-button @click="submit" :ripple="true" type="primary" shape="square" size="medium">提交</u-button>
		</view>
		<u-picker mode="region" ref="uPicker" v-model="show" @confirm="confirm" />
	</view>
</template>

<script>
	import amap from '../../../common/amap-wx.130.js';
	export default {
		data() {
			return {
				// 文字提示
				errorType: ['message'],
				show: false,
				form: {
					name: '',
					address: '',
					phone: '',
					province: '',
					city: '',
					county: '',
				},
				address: '',
				checked: false,
				edit: false, // true为编辑操作
				amapPlugin: null,
				//#ifdef H5 || MP-WEIXIN
				key: '5d0dcf6729ff147d7d2436d2effdd6ec',
				//#endif
				//#ifdef APP-PLUS
				key: '5d0dcf6729ff147d7d2436d2effdd6ec',
				//#endif
			};
		},
		onShow() {
			
		},
		async onLoad(option) {
			this.amapPlugin = new amap.AMapWX({
				key: this.key
			});
			//编辑地址 会带上id过来
			if (option.id) {
				const res = await this.$u.api.addressDetail(option.id)
				console.log('onLoad', res)
				this.form = res
				this.address = res.province + res.city + res.county
				this.checked = res.is_default == 1 ? true : false
				this.edit = true
			}else{
				// 获取定位信息
				////#ifdef H5 || MP-WEIXIN || APP-PLUS
				this.getRegeo();
				////#endif
			}
		},
		methods: {
			getRegeo() {
				uni.showLoading({
					title: '获取位置信息中'
				});
				console.log(this.key)
				this.amapPlugin.getRegeo({
					success: (data) => {
						uni.hideLoading();
						console.log('getRegeo:', data)
						this.form.address = data[0].name;
					},
					fail(info) {
						uni.showToast({
							title: info.errMsg,
							icon:'none'
							
						})
					}
				});
			},
			setDefault(e) {
				console.log('setDefault', e)
				this.checked = e.detail.value
			},
			showRegionPicker() {
				this.show = true;
			},
			confirm(e) {
				console.log('confirm', e)
				this.form.province = e.province.label
				this.form.city = e.city.label
				this.form.county = e.area.label
				this.address = e.province.label + e.city.label + e.area.label

			},
			// 提交
			async submit() {
				if (!this.form.name) {
					uni.showToast({
						icon: 'error',
						title: '收货人姓名不能为空'
					})
					return false
				}
				if (!this.form.phone) {
					uni.showToast({
						icon: 'error',
						title: '手机号码不能为空'
					})
					return false
				}
				if (!this.$u.test.mobile(this.form.phone)) {
					uni.showToast({
						icon: 'error',
						title: '手机格式不正确'
					})
					return false
				}
				if (!this.address) {
					uni.showToast({
						icon: 'error',
						title: '所在地区不能为空'
					})
					return false
				}
				if (!this.form.address) {
					uni.showToast({
						icon: 'error',
						title: '详细地址不能为空'
					})
					return false
				}
				// 新增收货地址
				if (this.checked) {
					this.form.is_default = 1
				}
				if (!this.edit) {
					const res = await this.$u.api.addressAdd(this.form);
					console.log('submitNo', res)
				} else {
					console.log(this.form)
					const res = await this.$u.api.addressEdit(this.form.id, this.form);
					console.log('submitNo', res)
				}
				uni.showToast({
					icon: 'success',
					title: '操作成功'
				})
				setTimeout(() => {
					uni.navigateTo({
						url: '/pages/cart/create'
					})
				}, 1500)

			}
		},
	};
</script>

<style lang="scss" scoped>
	/deep/ .line {
		color: $u-light-color;
		font-size: 25rpx;
	}

	.wrap {
		background-color: #f2f2f2;

		.top {
			background-color: #ffffff;
			border-top: solid 2rpx $u-border-color;
			padding: 22rpx;

			.item {
				display: flex;
				font-size: 25rpx;
				line-height: 100rpx;
				align-items: center;
				border-bottom: solid 2rpx $u-border-color;

				.left {
					width: 200rpx;
				}

				input {
					text-align: left;
				}
			}

			.address {
				padding: 20rpx 0;

				textarea {
					// width: 100%;
					height: 100rpx;
					background-color: #f7f7f7;
					line-height: 60rpx;
					margin: 40rpx auto;
					padding: 20rpx;
				}
			}

			.site-clipboard {
				padding-right: 40rpx;

				textarea {
					// width: 100%;
					height: 150rpx;
					background-color: #f7f7f7;
					line-height: 60rpx;
					margin: 40rpx auto;
					padding: 20rpx;
				}

				.clipboard {
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 26rpx;
					color: $u-tips-color;
					height: 80rpx;

					.icon {
						margin-top: 6rpx;
						margin-left: 10rpx;
					}
				}
			}
		}

		.bottom {
			margin-top: 20rpx;
			padding: 40rpx;
			padding-right: 0;
			background-color: #ffffff;
			font-size: 15rpx;

			.tag {
				display: flex;

				.left {
					width: 160rpx;
				}

				.right {
					display: flex;
					flex-wrap: wrap;

					.tags {
						width: 140rpx;
						padding: 16rpx 8rpx;
						border: solid 2rpx $u-border-color;
						text-align: center;
						border-radius: 50rpx;
						margin: 0 10rpx 20rpx;
						display: flex;
						font-size: 15rpx;
						align-items: center;
						justify-content: center;
						color: $u-content-color;
						line-height: 1;
					}

					.plus {
						//padding: 10rpx 0;
					}
				}
			}

			.default {
				margin-top: 50rpx;
				display: flex;
				justify-content: space-between;
				border-bottom: solid 2rpx $u-border-color;
				line-height: 64rpx;

				.tips {
					font-size: 15rpx;
				}

				.right {}
			}
		}
	}
</style>
